<?php
/*
 * author: Nguyen Viet Manh
 * date: 04-11-2010
 * email: manhnguyen@vietnambiz.com
 */

class MY_Model extends Model{
	
	var $_table = NULL;
	var $_id = NULL;
	var $_offset = 0;
	var $_limit = 20;
	var $_where = NULL;
	var $_sort_order = 'ASC';
	var $_sort_by = NULL;
	var $_order_by = NULL;
	var $_field_in = NULL;
	var $_where_in = NULL;
	var $_where_not_in = NULL;
	var $_sql = NULL;
	var $_select = '*';
	var $_distinct = false;
	
	function MY_Model(){
		parent::Model();
	}
	
	function set_table($table){$this->_table = $table;}
	function get_table(){return $this->_table;}
	
	function set_id($id){$this->_id = $id;}
	function get_id(){return $this->_id;}
	
	function set_select($value){$this->_select = $value;}
	function get_select(){return $this->_select;}
	
	function set_offset($offset){$this->_offset = $offset;}
	function get_offset(){return $this->_offset;}
	
	function set_limit($limit){$this->_limit = $limit;}
	function get_limit(){return $this->_limit;}
	
	function set_where($value){$this->_where = $value;}
	function get_where(){return $this->_where;}
	
	function set_field_in($value){$this->_field_in = $value;}
	function get_field_in(){return $this->_field_in;}
	
	function set_where_in($value){$this->_where_in = $value;}
	function get_where_in(){return $this->_where_in;}
	
	function set_where_not_in($where){$this->_where_not_in = $where;}
	function get_where_not_in(){return $this->_where_not_in;}
	
	function set_sql($sql){$this->_sql = $sql;}
	function get_sql(){return $this->_sql;}
	
	function set_order_by($order_by){$this->_order_by = $order_by;}
	function get_order_by(){return $this->_order_by;}
	
	function set_sort_by($value){$this->_sort_by = $value;}
	function get_sort_by(){return $this->_sort_by;}
	
	function set_sort_order($value){$this->_sort_order = $value;}
	function get_sort_order(){return $this->_sort_order;}
	
	function set_distinct($value){$this->_distinct = $value;}
	function get_distinct(){return $this->_distinct;}
	
	function insert($data=array()){
		if($data){
			if($this->db->insert($this->get_table(),$data))
				return true;
		}
		return false;
	}
	
	function update($data=array(),$id=NULL){
		if($data && $id){
			$this->db->where($this->get_id(),intval($id));
			if($this->db->update($this->get_table(),$data))
				return true;
		}
		return false;
	}
	
	function delete($id=NULL){
		if($id){
			$this->db->where($this->get_id(),intval($id));
			if($this->db->delete($this->get_table()))
				return true;
		}
		return false;
	}
	
	function row(){
		if($this->get_select()) $this->db->select($this->get_select());
		if($this->get_where()) $this->db->where($this->get_where());
		if($this->get_sql()) $this->db->where($this->get_sql());
		if($this->get_sort_by()) $this->db->order_by($this->get_sort_by().' '.$this->get_sort_order());
		if($this->get_order_by()) $this->db->order_by($this->get_order_by());
		$q = $this->db->get($this->get_table());
		return $q->row();
	}
	
	function result(){
		//get rows with limit
		if($this->get_distinct()) $this->db->distinct();
		$this->db->select($this->get_select())
				 ->from($this->get_table());
		if($this->get_where()) $this->db->where($this->get_where());
		if($this->get_sql()) $this->db->where($this->get_sql());
		if($this->get_where_in()) $this->db->where_in($this->get_field_in(),$this->get_where_in());
		if($this->get_where_not_in()) $this->db->where_not_in($this->get_field_in(),$this->get_where_not_in());
		if($this->get_sort_by()) $this->db->order_by($this->get_sort_by().' '.$this->get_sort_order());
		if($this->get_order_by()) $this->db->order_by($this->get_order_by());
		$this->db->limit($this->get_limit(),$this->get_offset());
		$q = $this->db->get();
		$result['rows'] = $q->result();
		//get num_rows
		$this->db->select('count('.$this->get_id().') as cid')
				 ->from($this->get_table());
		if($this->get_where())$this->db->where($this->get_where());
		if($this->get_sql())$this->db->where($this->get_sql());
		$q = $this->db->get();
		$result['num_rows']	= $q->row()->cid;
		return $result;
	}
}